package frontend.pages.settings.personalprofile;

import backend.model.other.User;
import backend.service.other.UserService;
import frontend.core.TimeTrackingWebApplication;
import frontend.core.TimeTrackingWebSession;
import frontend.model.settings.SettingsUIModel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import static frontend.pages.admin.base.AbstractManagingForm.PAGE_PARAMETER_KEY_OPERATION_SUCCESSFUL;

public class PersonalProfilePanel extends Panel {

    public PersonalProfilePanel(String id, PageParameters pageParameters) {
        super(id);
        setOutputMarkupId(true);
        buildFormContainer(pageParameters);
    }

    private void buildFormContainer(PageParameters pageParameters) {
        String loggedInUsername = (String) TimeTrackingWebSession.get().getAttribute(TimeTrackingWebSession.LOGIN_USERNAME_ATTR);
        User loggedInUser = TimeTrackingWebApplication.getService(UserService.class).findUserByUsername(loggedInUsername);
        add(new PersonalProfileForm("personalProfileForm", new SettingsUIModel(loggedInUser)));
        setOutputMarkupId(true);
        setOutputMarkupPlaceholderTag(true);
        if (pageParameters != null) {
            info(pageParameters.get(PAGE_PARAMETER_KEY_OPERATION_SUCCESSFUL));
        }
    }

}
